<template>
  <a-modal
    :open="visible"
    title="顾客押金"
    width="800px"
    @cancel="handleCancel"
    :footer="null"
  >
    <div class="deposit-management">
      <!-- 押金余额 -->
      <div class="deposit-balance">
        <h3>押金余额 ¥{{ customer?.deposit.toFixed(2) || '0.00' }}</h3>
        <div class="deposit-actions">
          <a-button @click="showRefundModal">退还</a-button>
          <a-button type="primary" @click="showTopupModal">充值</a-button>
        </div>
      </div>

      <!-- 使用记录 -->
      <div class="usage-record">
        <h4>使用记录</h4>
        <a-table
          :columns="recordColumns"
          :data-source="depositRecords"
          :pagination="false"
          size="small"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'type'">
              <a-tag :color="record.type === 'topup' ? 'green' : 'red'">
                {{ record.type === 'topup' ? '充值' : '退还' }}
              </a-tag>
            </template>
            <template v-else-if="column.key === 'amount'">
              <span
                :class="
                  record.type === 'topup'
                    ? 'amount-positive'
                    : 'amount-negative'
                "
              >
                {{ record.type === 'topup' ? '+' : '-' }}¥{{
                  record.amount.toFixed(2)
                }}
              </span>
            </template>
          </template>
        </a-table>
      </div>

      <!-- 打印选项 -->
      <div class="print-options">
        <a-space>
          <a-checkbox v-model:checked="autoPrint">充值完成自动打印</a-checkbox>
          <a-button @click="printRecord">打印单条</a-button>
        </a-space>
      </div>
    </div>

    <!-- 充值模态框 -->
    <a-modal
      :open="topupModalVisible"
      title="押金充值"
      width="600px"
      @ok="handleTopup"
      @cancel="closeTopupModal"
    >
      <div class="topup-form">
        <div class="customer-info">
          <p><strong>顾客姓名:</strong> {{ customer?.name }}</p>
          <p><strong>电话:</strong> {{ customer?.phone }}</p>
        </div>

        <a-form layout="vertical">
          <a-form-item label="充值方式">
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item label="微信">
                  <a-input-number
                    v-model:value="topupForm.wechat"
                    placeholder="元"
                    :min="0"
                    :precision="2"
                    style="width: 100%"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="支付宝">
                  <a-input-number
                    v-model:value="topupForm.alipay"
                    placeholder="元"
                    :min="0"
                    :precision="2"
                    style="width: 100%"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item label="现金">
                  <a-input-number
                    v-model:value="topupForm.cash"
                    placeholder="元"
                    :min="0"
                    :precision="2"
                    style="width: 100%"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item label="银行卡">
                  <a-input-number
                    v-model:value="topupForm.bankCard"
                    placeholder="元"
                    :min="0"
                    :precision="2"
                    style="width: 100%"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form-item>

          <a-form-item label="充值金额">
            <div class="total-amount">¥{{ totalTopupAmount.toFixed(2) }}</div>
          </a-form-item>

          <a-form-item>
            <a-space>
              <a-checkbox v-model:checked="autoPrintReceipt"
                >充值完成自动打印</a-checkbox
              >
              <a-button @click="printReceipt">打印</a-button>
            </a-space>
            <div class="form-tip">打印充值单据,云端智能模板,下载打印</div>
          </a-form-item>
        </a-form>
      </div>
    </a-modal>

    <!-- 退还模态框 -->
    <a-modal
      :open="refundModalVisible"
      title="押金退还"
      width="500px"
      @ok="handleRefund"
      @cancel="closeRefundModal"
    >
      <div class="refund-form">
        <div class="customer-info">
          <p><strong>顾客姓名:</strong> {{ customer?.name }}</p>
          <p><strong>当前押金:</strong> ¥{{ customer?.deposit.toFixed(2) }}</p>
        </div>

        <a-form layout="vertical">
          <a-form-item label="退还金额" name="refundAmount">
            <a-input-number
              v-model:value="refundForm.amount"
              placeholder="请输入退还金额"
              :min="0"
              :max="customer?.deposit || 0"
              :precision="2"
              style="width: 100%"
            />
          </a-form-item>

          <a-form-item label="退还方式" name="refundMethod">
            <a-select
              v-model:value="refundForm.method"
              placeholder="请选择退还方式"
            >
              <a-select-option value="wechat">微信</a-select-option>
              <a-select-option value="alipay">支付宝</a-select-option>
              <a-select-option value="cash">现金</a-select-option>
              <a-select-option value="bankCard">银行卡</a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item label="备注" name="refundNotes">
            <a-textarea
              v-model:value="refundForm.notes"
              placeholder="请输入备注信息"
              :rows="3"
            />
          </a-form-item>
        </a-form>
      </div>
    </a-modal>
  </a-modal>
</template>

<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { message } from 'ant-design-vue';

interface Props {
  visible: boolean;
  customer?: any;
}

interface Emits {
  (e: 'update:visible',value: boolean): void;
  (e: 'success'): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 押金记录表格列定义
const recordColumns = [
  {
    title: '日期',
    dataIndex: 'date',
    key: 'date',
    width: 150,
  },
  {
    title: '充值/退还',
    dataIndex: 'type',
    key: 'type',
    width: 100,
  },
  {
    title: '充值/退还金额',
    dataIndex: 'amount',
    key: 'amount',
    width: 120,
  },
  {
    title: '余额',
    dataIndex: 'balance',
    key: 'balance',
    width: 100,
  },
];

// 押金记录数据
const depositRecords = ref([
  {
    id: '1',
    date: '2019-08-07 11:51',
    type: 'refund',
    amount: 50.0,
    balance: 50.0,
  },
  {
    id: '2',
    date: '2019-08-07 11:51',
    type: 'topup',
    amount: 100.0,
    balance: 100.0,
  },
]);

// 充值模态框
const topupModalVisible = ref(false);
const topupForm = ref({
  wechat: 0,
  alipay: 0,
  cash: 0,
  bankCard: 0,
});
const autoPrintReceipt = ref(false);

// 退还模态框
const refundModalVisible = ref(false);
const refundForm = ref({
  amount: 0,
  method: '',
  notes: '',
});

// 打印选项
const autoPrint = ref(false);

// 计算属性
const totalTopupAmount = computed(() => {
  return (
    topupForm.value.wechat +
    topupForm.value.alipay +
    topupForm.value.cash +
    topupForm.value.bankCard
  );
});

// 方法
const showTopupModal = () => {
  topupModalVisible.value = true;
  resetTopupForm();
};

const closeTopupModal = () => {
  topupModalVisible.value = false;
  resetTopupForm();
};

const resetTopupForm = () => {
  topupForm.value = {
    wechat: 0,
    alipay: 0,
    cash: 0,
    bankCard: 0,
  };
};

const handleTopup = () => {
  if (totalTopupAmount.value <= 0) {
    message.warning('请输入充值金额');
    return;
  }

  // 这里可以调用API进行充值
  message.success(`充值成功，金额: ¥${totalTopupAmount.value.toFixed(2)}`);

  // 添加充值记录
  depositRecords.value.unshift({
    id: Date.now().toString(),
    date: new Date().toLocaleString(),
    type: 'topup',
    amount: totalTopupAmount.value,
    balance: (props.customer?.deposit || 0) + totalTopupAmount.value,
  });

  closeTopupModal();
  emit('success');
};

const showRefundModal = () => {
  refundModalVisible.value = true;
  refundForm.value.amount = props.customer?.deposit || 0;
};

const closeRefundModal = () => {
  refundModalVisible.value = false;
  resetRefundForm();
};

const resetRefundForm = () => {
  refundForm.value = {
    amount: 0,
    method: '',
    notes: '',
  };
};

const handleRefund = () => {
  if (refundForm.value.amount <= 0) {
    message.warning('请输入退还金额');
    return;
  }

  if (!refundForm.value.method) {
    message.warning('请选择退还方式');
    return;
  }

  // 这里可以调用API进行退还
  message.success(`退还成功，金额: ¥${refundForm.value.amount.toFixed(2)}`);

  // 添加退还记录
  depositRecords.value.unshift({
    id: Date.now().toString(),
    date: new Date().toLocaleString(),
    type: 'refund',
    amount: refundForm.value.amount,
    balance: (props.customer?.deposit || 0) - refundForm.value.amount,
  });

  closeRefundModal();
  emit('success');
};

const printRecord = () => {
  message.info('打印押金记录');
};

const printReceipt = () => {
  message.info('打印充值单据');
};

const handleCancel = () => {
  emit('update:visible', false);
};
</script>

<style scoped>
.deposit-management {
  max-height: 600px;
  overflow-y: auto;
}

.deposit-balance {
  text-align: center;
  padding: 24px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-bottom: 24px;
}

.deposit-balance h3 {
  margin: 0 0 16px;
  font-size: 24px;
  color: #1890ff;
}

.deposit-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.usage-record {
  margin-bottom: 24px;
}

.usage-record h4 {
  margin-bottom: 16px;
  font-size: 16px;
  font-weight: 600;
}

.print-options {
  padding: 16px 0;
  border-top: 1px solid #f0f0f0;
}

.amount-positive {
  color: #52c41a;
  font-weight: 600;
}

.amount-negative {
  color: #ff4d4f;
  font-weight: 600;
}

.topup-form,
.refund-form {
  padding: 16px 0;
}

.customer-info {
  background: #f8f9fa;
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 16px;
}

.customer-info p {
  margin: 0 0 4px;
}

.total-amount {
  font-size: 20px;
  font-weight: 700;
  color: #52c41a;
  text-align: center;
  padding: 12px;
  background: #f6ffed;
  border-radius: 6px;
}

.form-tip {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

:deep(.ant-table-thead > tr > th) {
  background: #fafafa;
  font-weight: 600;
}
</style>
